package top.heue.test.http

import com.hjq.http.EasyConfig
import com.hjq.http.config.IRequestBodyStrategy
import com.hjq.http.config.IRequestServer
import com.hjq.http.model.RequestBodyType
import okhttp3.OkHttpClient
import top.heue.test.app.App

object Http {

    private val server by lazy { RequestServer() }
    private val client by lazy { OkHttpClient.Builder().build() }

    fun init(app: App) {
        EasyConfig.with(client)
            // 是否打印日志
            //.setLogEnabled(BuildConfig.DEBUG)
            // 设置服务器配置（必须设置）
            .setServer(server)
            // 设置请求处理策略（必须设置）
            .setHandler(RequestHandler(app))
            // 设置请求重试次数
            .setRetryCount(3)
            // 添加全局请求参数
            //.addParam("token", "6666666")
            // 添加全局请求头
            //.addHeader("time", "20191030")
            // 启用配置
            .into()
    }

    class RequestServer: IRequestServer {
        override fun getHost(): String = "https://api.heue.top"
        override fun getBodyType(): IRequestBodyStrategy {
            return RequestBodyType.JSON
        }
    }
}

/*
*
*
public final class LoginApi implements IRequestApi {

    @NonNull
    @Override
    public String getApi() {
        return "user/login";
    }

    /** 用户名 */
    private String userName;

    /** 登录密码 */
    private String password;

    public LoginApi setUserName(String userName) {
        this.userName = userName;
        return this;
    }

    public LoginApi setPassword(String password) {
        this.password = password;
        return this;
    }
}
* */

/*
*
*
EasyHttp.post(this)
        .api(new LoginApi()
                .setUserName("Android 轮子哥")
                .setPassword("123456"))
        .request(new HttpCallbackProxy<HttpData<LoginBean>>(activity) {

            @Override
            public void onHttpSuccess(HttpData<LoginBean> data) {
                toast("登录成功");
            }
        });
* */

/*
*
*
EasyHttp.download(this)
        .method(HttpMethod.GET)
        .file(new File(Environment.getExternalStorageDirectory(), "微信.apk"))
        //.url("https://qd.myapp.com/myapp/qqteam/AndroidQQ/mobileqq_android.apk")
        .url("http://dldir1.qq.com/weixin/android/weixin708android1540.apk")
        .md5("2E8BDD7686474A7BC4A51ADC3667CABF")
        .listener(new OnDownloadListener() {

            @Override
            public void onDownloadStart(File file) {
                mProgressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onDownloadProgressChange(File file, int progress) {
                mProgressBar.setProgress(progress);
            }

            @Override
            public void onDownloadSuccess(File file) {
                toast("下载完成：" + file.getPath());
                installApk(XxxActivity.this, file);
            }

            @Override
            public void onDownloadFail(File file, Throwable throwable) {
                toast("下载出错：" + throwable.getMessage());
            }

            @Override
            public void onDownloadEnd(File file) {
                mProgressBar.setVisibility(View.GONE);
            }

        }).start();
* */